package com.sywl.glzh.web;

import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sywl.glzh.domain.About;
import com.sywl.glzh.domain.Contact;
import com.sywl.glzh.domain.Information;
import com.sywl.glzh.domain.Service;

@Controller
public class IndexController {
	
	/**
	 * 首页
	 * @param model
	 * @return
	 */
	@RequestMapping({"/", "index", "default"})
	public String index(Model model) {
		return "home";
	}
	     
	/**
	 * 首页 英文  
	 * @param model
	 * @return
	 */   
	@RequestMapping({"/en/", "en/index", "en/default"})
	public String enIndex(Model model) {
		return "en/" + index(model);
	}
	
	/**
	 * 关于我们
	 * @param model
	 * @return
	 */
	@RequestMapping({"about", "en/about"})
	public void about(Model model) {
		List<About> list = About.findAllAbouts();
		if(list.size() > 0) {
			model.addAttribute("about", list.get(0));
		}
	}
	
	/**
	 * 联系我们
	 * @param model
	 * @return
	 */
	@RequestMapping({"contact", "en/contact"})
	public void contact(Model model) {
		List<Contact> list = Contact.findAllContacts();
		if(list.size() > 0) {
			model.addAttribute("contact", list.get(0));
		}
	}
	
	/**
	 * 下载中心
	 * @param model
	 * @return
	 */
	@RequestMapping({"download", "en/download"})
	public void info(Model model) {
		model.addAttribute("downloads", Information.findAllInformations());
	}
	
	/**
	 * 更新资料下载次数
	 * @param id
	 * @return
	 */
	@RequestMapping("down/{id}")
	@ResponseBody
	public String down(@PathVariable long id) {
		Information o = Information.findInformation(id);
		if(o.getDownloads() == null) o.setDownloads(0L);
		o.setDownloads(o.getDownloads() + 1);
		o.merge();
		return "";
	}
	
	/**
	 * 客户服务
	 * @param model
	 * @return
	 */
	@RequestMapping({"service", "en/service"})
	public void service(Model model) {
		List<Service> list = Service.findAllServices();
		if(list.size() > 0) {
			model.addAttribute("service", list.get(0));
		}
	}
	
}
